package com.yunxin.admin.system.infra.persistence.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunxin.admin.system.domain.model.dict.Dict;
import com.yunxin.admin.system.domain.model.dict.DictRepository;
import com.yunxin.admin.system.infra.persistence.converter.DictConverter;
import com.yunxin.admin.system.infra.persistence.entity.SysDictDO;
import com.yunxin.admin.system.infra.persistence.mapper.SysDictMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * @author zero°
 * @since 2023-07-11
 */
@Repository
@RequiredArgsConstructor
public class DictRepositoryImpl extends ServiceImpl<SysDictMapper, SysDictDO> implements DictRepository, IService<SysDictDO> {

    private final DictConverter dictConverter;

    @Override
    public boolean saveDict(Dict dict) {
        SysDictDO dictDO = dictConverter.fromDict(dict);
        return this.save(dictDO);
    }

    @Override
    public boolean updateDict(Dict dict) {
        SysDictDO dictDO = dictConverter.fromDict(dict);
        return this.updateById(dictDO);
    }

    @Override
    public boolean removeByIdList(Collection<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public boolean updateDictCode(String oldCode, String newCode) {
        return this.update(new LambdaUpdateWrapper<SysDictDO>()
                .eq(SysDictDO::getTypeCode, oldCode)
                .set(SysDictDO::getTypeCode, newCode)
        );
    }

    @Override
    public boolean removeByTypeCodes(Collection<String> dictTypeCodes) {
        return this.remove(new LambdaQueryWrapper<SysDictDO>()
                .in(SysDictDO::getTypeCode, dictTypeCodes));
    }


}
